package com.leanyuan.Infor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leanyuan.Infor.api.Result;
import com.leanyuan.publi.domain.Building;
import com.leanyuan.publi.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/loudong")
public class LouDongController {
    @Autowired(required = false)
    BuildingService buildingService;

    @RequestMapping("/del")
    public Result<Object> del(@RequestBody Map<String,Integer> params){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("building_id",params.get("id"));
        buildingService.remove(wrapper);
        return Result.ok();
    }
    @RequestMapping("/Info")
    public Result<Object> findAll(@RequestBody Map<String,Object> params){
        Page<Building> page1=new Page<>(Long.valueOf(String.valueOf(params.get("page"))) ,Long.valueOf(String.valueOf(params.get("pageSize"))));
        Integer id=null;
        String name=null;
        if(params.get("id")!=null&&params.get("id")!=""){
            id = Integer.valueOf(String.valueOf(params.get("id")));
        }
        if(params.get("lname")!=null&&params.get("lname")!=""){
            name = String.valueOf(params.get("lname"));
        };
        IPage page2=buildingService.findAllp(page1,id,name);
        return Result.ok(page2);
    }

    @RequestMapping("/add")
    public Result<Object> add(@RequestBody Map<String,Object> params){
        System.out.println(params);
        Building building=new Building(Integer.valueOf(String.valueOf(params.get("id"))),Integer.valueOf(String.valueOf(params.get("adminid"))),
                String.valueOf(params.get("lname")),Integer.valueOf(String.valueOf(params.get("area"))),new Date());
        Boolean flag;
        //如果有重复主键报异常
        try {
            flag=buildingService.save(building);
        }catch (Exception e){
            flag=false;
        }

       if(flag){
           return Result.ok();
       }return Result.error("添加失败");
    }

    @RequestMapping("/upd")
    public Result<Object> upd(@RequestBody Map<String,Object> params){
        Building building=new Building(Integer.valueOf(String.valueOf(params.get("id"))),
                String.valueOf(params.get("lname")),Integer.valueOf(String.valueOf(params.get("area"))));
        QueryWrapper<Building> wrapper=new QueryWrapper<>();
        wrapper.eq("building_id",Integer.valueOf(String.valueOf(params.get("id"))));
        Boolean flag=buildingService.update(building,wrapper);
        if(flag){
            return Result.ok();
        }return Result.error("修改失败");
    }
}
